<?php
defined('SYSPATH') or die('No direct script access.');

class Model_Consult extends Kohana_Model {

    public function sendChat($p_id,$c_id,$text){        
        $sql = "SELECT * FROM consult_status where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0){
            $oldText = $query[0]['consult_chat'];
            if($oldText==""){
                $newText = $text;
            }else{
                $newText = $oldText ."<br>".$text;
            }
            DB::update('consult_status')
                ->set(array('consult_chat'=>$newText))
                ->where('c_id','=', $c_id)
                ->and_where('p_id', '=',$p_id)
                ->execute();
        }
    }

    public function loadChat($p_id,$c_id){
        $sql = "SELECT * FROM consult_status where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0){
            return $query[0]['consult_chat'];
        }
        return null;
    }

    public function getConsultID($p_id) {
        $c_id = 0;
        $sql = "SELECT * FROM consult_status where p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

        if(count($query)==0){
            //generate new consult id
            $sql = "SELECT max(c_id) as c FROM consult_history where p_id=".$p_id;
            $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

            if($query[0]['c']==null) {
                $c_id = 1;
            }else {
                $c_id = $query[0]['c'] + 1;
            }
        }else {
            $c_id = $query[0]['c_id'];
        }
        return $c_id;
    }

    public function getWaitingPatient($consult_doctor_id){
        $sql = "SELECT consult_status.*,patient_profile.* FROM consult_status,patient_profile where consult_status.status = 'Waiting to consult' and consult_status.consult_doctor_id=".$consult_doctor_id." and consult_status.p_id=patient_profile.p_id";
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query;
        }else {           
            return null;
        }
    }

    public function getConsultingPatient($consult_doctor_id){
        $sql = "SELECT consult_status.*,patient_profile.* FROM consult_status,patient_profile where consult_status.status = 'Consulting' and consult_status.consult_doctor_id=".$consult_doctor_id." and consult_status.p_id=patient_profile.p_id";
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query;
        }else {
            return null;
        }
    }

    public function updateStartNIHSS($p_id,$c_id) {
        DB::update('nihss')
                ->set(array(
                'nihss_start'=>DB::expr('NOW()'),
                ))
                ->where('c_id','=', $c_id)
                ->and_where('p_id', '=',$p_id)
                ->execute();

        return date('Y-m-d H:i:s');
    }

    public function updateEndNIHSS($p_id,$c_id) {
        DB::update('nihss')
                ->set(array(
                'nihss_end'=>DB::expr('NOW()'),
                ))
                ->where('c_id','=', $c_id)
                ->and_where('p_id', '=',$p_id)
                ->execute();
        return date('Y-m-d H:i:s');
    }

    public function getTimeUseNIHSS($p_id,$c_id){
        $sql = "SELECT * FROM nihss where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            $timedif = $this->TimeDifference($query[0]['nihss_start'], $query[0]['nihss_end']);
            return "<font color=blue>$timedif[0] ชั่วโมง $timedif[1] นาที $timedif[2] วินาที</font>";
        }else {           
            return null;
        }
    }

    public function TimeDifference($date1, $date2){
        $date1 = is_int($date1) ? $date1 : strtotime($date1);
        $date2 = is_int($date2) ? $date2 : strtotime($date2);

        if (($date1 !== false) && ($date2 !== false)) {
            if ($date2 >= $date1) {
                $diff = ($date2 - $date1);

                if ($days = intval((floor($diff / 86400))))
                    $diff %= 86400;
                if ($hours = intval((floor($diff / 3600))))
                    $diff %= 3600;
                if ($minutes = intval((floor($diff / 60))))
                    $diff %= 60;

                return array($hours, $minutes, intval($diff));
            }
        }

        return false;
    }

    public function getConsultHistory($p_id) {
        $sql = "SELECT consult_history. * , patient_place. * , consult_doctor. *
                FROM consult_history, patient_place, consult_doctor
                WHERE p_id =".$p_id."
                AND consult_history.pp_id = patient_place.pp_id
                AND consult_history.consult_doctor_id = consult_doctor.consult_doctor_id
                ORDER BY c_id desc";
        
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query;
        }else {
            return null;
        }
    }

    public function getConsultHistoryByConsultDoctorId($consult_doctor_id) {
        $sql = "SELECT consult_history. * , patient_place. * , consult_doctor. *,patient_profile.*
                FROM consult_history, patient_place, consult_doctor,patient_profile
                WHERE consult_history.consult_doctor_id =".$consult_doctor_id."
                AND consult_history.p_id = patient_profile.p_id
                AND consult_history.pp_id = patient_place.pp_id 
                AND consult_history.consult_doctor_id = consult_doctor.consult_doctor_id
                ORDER BY c_id desc";

        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query;
        }else {
            return null;
        }
    }

    public function getConsultHistoryByCID($p_id,$c_id) {
        $sql = "SELECT consult_history. * , patient_place. * , consult_doctor. *
                FROM consult_history, patient_place, consult_doctor
                WHERE p_id =".$p_id."
                AND c_id = ".$c_id."
                AND consult_history.pp_id = patient_place.pp_id
                AND consult_history.consult_doctor_id = consult_doctor.consult_doctor_id";

        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0];
        }else {
            return null;
        }
    }

    public function loadLaboratoryData($p_id,$c_id) {
        /*$lab['CTscanYN'] = "on"; $lab['CTMRI'] = 10; $lab['CTASPECT'] = 0; $lab['CTNoICHYN'] = "on";
        $lab['CTnobrainYN'] = "on"; $lab['CTPerfusionYN'] = "on"; $lab['MRIYN'] = "on"; $lab['MRIResult'] = 0;
        $lab['MRINoICHYN'] = "on"; $lab['MRInobrainYN'] = "on"; $lab['MRIDWI30YN'] = "on"; $lab['MRIDWIPWIYN'] = "on";
        $lab['MRAYN'] = "on"; $lab['MRAMCAYN'] = "on"; $lab['MRAMCAso'] = "s"; $lab['MRAACAYN'] = "on";
        $lab['MRAACAso'] = "s";  $lab['MRABAYN'] = "on"; $lab['MRABAso'] = "s"; $lab['MRAICAYN'] = "on";
        $lab['MRAICAso'] = "s/o"; $lab['MRAotherYN'] = "on"; $lab['MRAother'] = "text"; $lab['BloodPG'] = 0;
        $lab['BloodPT'] = 0; $lab['BloodPTT'] = 0; $lab['BloodPLT'] = 0; $lab['BloodPGYN'] = "on"; $lab['BloodINRYN'] = "on";
        $lab['BloodPTTYN'] = "on";$lab['BloodPLTYN'] = "on";*/

        $sql = "SELECT * FROM laboratory_data where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0];
        }else {
            //echo "not found".$p_id.$c_id.$sql;
            return null;
        }
    }

    public function loadStrokePhy($p_id,$c_id) {
        /*$stp['BPmin'] = 0;$stp['BPmax'] = 0;$stp['PR'] = 0;$stp['PRminYN'] = "y";$stp['CarotidLt'] = "y";
        $stp['CarotidRt'] = "y"; $stp['Heart'] = "text"; $stp['ConsciousnessE'] = 1; $stp['ConsciousnessM'] = 1;
        $stp['ConsciousnessV'] = 1; $stp['OrientationTime'] = "var"; $stp['OrientationPlace'] = "var"; $stp['OrientationPerson'] = "var";
        $stp['Speech'] = "d"; $stp['Aphasia'] = "m"; $stp['Fluency'] = "n"; $stp['Comprehension'] = "n"; $stp['Paraphasia'] = "n";
        $stp['Repetition'] = "n"; $stp['Naming'] = "n"; $stp['CranialL'] = "var"; $stp['CranialR'] = "var";
        $stp['Horner-+'] = "-"; $stp['HornerLR'] = "l"; $stp['HornerType'] = "var"; $stp['PupilsLt'] = 0;
        $stp['PupilsLtc'] = "g"; $stp['PupilsRt'] = 0; $stp['PupilsRtc'] = "g"; $stp['VALt'] = 0;
        $stp['VARt'] = 0; $stp['VFLt'] = "var"; $stp['VFRt'] = "var"; $stp['FacialWeaknessLR'] = "l";
        $stp['FacialWeaknessUL'] = "u"; $stp['TongueDeviationYN'] = "y"; $stp['TongueDeviationLR'] = "l";
        $stp['GagReflexL'] = 0; $stp['GagReflexR'] = 0; $stp['UvalaMD'] = "m"; $stp['UvalaLR'] = "l";
        $stp['MotorLTp'] = 0; $stp['MotorLTd'] = 0; $stp['MotorLBp'] = 0;$stp['MotorLBd'] = 0;
        $stp['MotorRTp'] = 0; $stp['MotorRTd'] = 0; $stp['MotorRBp'] = 0; $stp['MotorRBd'] = 0;
        $stp['PronatorDriftingLR'] = "l";$stp['CerebellarYN'] = "y"; $stp['CerebellarGLR'] = "l";
        $stp['CerebellarTYN'] = "y"; $stp['CerebellarFYN'] = "y";$stp['SensoryTMD'] = "t"; $stp['NeglectLt'] = 0;
        $stp['NeglectRt'] = 0; $stp['NeglectBBK'] = 0; $stp['ProprioceptionLt'] = 0; $stp['ProprioceptionRt'] = 0;
        $stp['StereognosisLt'] = 0;$stp['StereognosisRt'] = 0; $stp['FigurePalmTestLt'] = 0; $stp['FigurePalmTestRt'] = 0;
        $stp['FinalDXHI'] = "h"; $stp['FinalDXHType'] = "type"; $stp['VesselAP'] = "a"; $stp['VesselAc'] = "a";
        $stp['VesselPc'] ="pc";$stp['Cause'] = "e"; $stp['CauseE'] = "c"; $stp['CauseLType'] = ""; $stp['CauseOther'] = "other";
        $stp['Stage'] = "s"; $stp['stp_others'] = "text"; $stp['ProvisionalDiagnosis'] = "text"; $stp['CTScan'] = "text";
        $stp['RX'] = "text";
        $stp['DTRL0'] = 0; $stp['DTRL1'] = 0; $stp['DTRL2'] = 0; $stp['DTRL3'] = 0;$stp['DTRR0'] = 0; $stp['DTRR1'] = 0;$stp['DTRR2'] = 0;
        $stp['DTRR3'] = 0; $stp['DTRLB'] = 0; $stp['DTRRB'] = 0;*/
        $sql = "SELECT * FROM stroke_phy_data where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0];
        }else {
            //echo "not found";
            return null;
        }
    }

    public function loadStrokeData($p_id,$c_id) {
        //Stoke data
        /*$std['cc'] = "test"; $std['PI'] = "test"; $std['headache'] = "no";  $std['sideheadache'] = "r";
            $std['muchheadache'] = "mr"; $std['areaheadache'] = "f"; $std['areaheadache_other'] = "test";
            $std['ph_ht_yn'] = "yes"; $std['ph_ht_yrs'] = "1"; $std['ph_ht_med'] = "test"; $std['ph_dm_yn'] = "yes";
            $std['ph_dm_yrs'] = "1"; $std['ph_dm_med'] = "test"; $std['ph_dyslipidemia_yn'] = "yes";
            $std['ph_dyslipidemia_yrs'] = "1"; $std['ph_dyslipidemia_med'] = "test"; $std['ph_smoking_yn'] = "yes";
            $std['ph_smoking_pack'] = "1"; $std['ph_smoking_yrs'] = "1"; $std['ph_smoking_qyrs'] = "2";
            $std['ph_heart_disease_yn'] = "yes"; $std['ph_heart_disease_type'] = "type"; $std['ph_heart_disease_med'] = "med";
            $std['ph_others_yn'] = "yes"; $std['ph_others'] = "other";*/

        $sql = "SELECT * FROM stroke_data where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0];
        }else {
            //echo "not found";
            return null;
        }
    }



    public function loadEstimateData($p_id,$c_id) {
        $sql = "SELECT * FROM estimate_data where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0];
        }else {
            return null;
        }
    }

    public function getIPCameraData($p_id,$c_id){
        $sql = "SELECT * FROM consult_status where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            $pp_id =  $query[0]['pp_id'];
            $sql = "SELECT * FROM patient_place where pp_id=".$pp_id;
            $query2 = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
            if(count($query)!=0) {
                return $query2[0];
            }else{
                return null;
            }
        }else {
            return null;
        }
    }

    public function getDoctorData($p_id,$c_id){
        $sql = "SELECT * FROM consult_status where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            $consult_doctor_id =  $query[0]['consult_doctor_id'];
            $sql = "SELECT * FROM consult_doctor where consult_doctor_id=".$consult_doctor_id;
            $query2 = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
            if(count($query)!=0) {
                return $query2[0];
            }else{
                return null;
            }
        }else {
            return null;
        }
    }

    public function loadManagementData($p_id,$c_id) {
        $sql = "SELECT * FROM management_data where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0];
        }else {
            //echo "not found".$p_id.$c_id.$sql;
            return "";
        }
    }

    public function updateSuggestionManagement($p_id,$c_id,$data) {
        DB::update('management_data')
                ->set(array(
                'sm_IVTYn'=>$data['sm_IVTYn'],
                'sm_IVTStartTime'=>$data['sm_IVTStartTime'],
                'sm_NIVTYn'=>$data['sm_NIVTYn'],
                'sm_NIVTC'=>$data['sm_NIVTC'],
                'sm_OtherYN'=>$data['sm_OtherYN'],
                'sm_OtherTxt'=>$data['sm_OtherTxt']
                ))
                ->where('c_id','=', $c_id)
                ->and_where('p_id', '=',$p_id)
                ->execute();
    }

    public function loadNIHSSData($p_id,$c_id) {
        $sql = "SELECT * FROM nihss where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0];
        }else {
            //echo "not found".$p_id.$c_id.$sql;
            return null;
        }
    }

    public function isConsulting($c_id) {
        $sql = "SELECT * FROM consult_status where c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)==0) {
            return false;
        }else {
            return true;
        }
    }

    public function updateStatus($p_id,$c_id,$status) {

        $sql = "SELECT * FROM consult_status where p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)==0) {
            DB::insert('consult_status',array('p_id','c_id','status','consult_start'))
                    ->values(array($p_id,$c_id,$status,DB::expr('NOW()')))->execute();
        }else {
            DB::update('consult_status')
                    ->set(array('status'=>$status))
                    ->where('c_id','=', $c_id)
                    ->execute();
        }
    }

    public function updatePrecontract($p_id,$c_id,$data) {
        DB::update('consult_status')
                ->set(array(
                'pp_id'=>$data['pp_id'],
                'doctor_name'=>$data['doctor_name'],
                'consult_doctor_id'=>'0',
                'status'=>"select_doctor"
                ))
                ->where('c_id','=', $c_id)
                ->and_where('p_id', '=',$p_id)
                ->execute();
    }

    public function getAllConsultDoctor() {
        $sql = "SELECT * FROM consult_doctor";
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        return $query;
    }

    public function updateManagementData($p_id,$c_id,$data) {
        $table = "management_data";
        $sql = "SELECT * FROM ".$table." where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            //update
            DB::update($table)
                    ->set(array(
                        'im_IVfluidYN'=>isset($data['im_IVfluidYN'])?$data['im_IVfluidYN']:"",
                        'im_O2CanulaYN'=>isset($data['im_O2CanulaYN'])?$data['im_O2CanulaYN']:"",
                        'im_IntubationYN'=>isset($data['im_IntubationYN'])?$data['im_IntubationYN']:"",
                        'im_OtherYn'=>isset($data['im_OtherYn'])?$data['im_OtherYn']:"",
                        'im_OtherTxt'=>isset($data['im_OtherTxt'])?$data['im_OtherTxt']:"",
                    ))
                    ->where('p_id','=', $p_id)
                    ->and_where('c_id', '=',$c_id)
                    ->execute();
        }else {
            print_r($data);
            DB::insert($table,
                    array(
                        'p_id' ,'c_id' ,
                        'im_IVfluidYN','im_O2CanulaYN','im_IntubationYN','im_OtherYn','im_OtherTxt'
                    ))
                    ->values(array(
                        $p_id,$c_id,
                        isset($data['im_IVfluidYN'])?$data['im_IVfluidYN']:"",
                        isset($data['im_O2CanulaYN'])?$data['im_O2CanulaYN']:"",
                        isset($data['im_IntubationYN'])?$data['im_IntubationYN']:"",
                        isset($data['im_OtherYn'])?$data['im_OtherYn']:"",
                        isset($data['im_OtherTxt'])?$data['im_OtherTxt']:""
                    ))
                    ->execute();
        }
    }

    public function updateEstimateData($p_id,$c_id,$data) {
        $table = "estimate_data";
        $this->updateEstimate($p_id, $c_id, $data, $table);
    }

    public function updateEstimateCData($p_id,$c_id,$data) {
        $table = "estimate_data_c";
        $this->updateEstimate($p_id, $c_id, $data, $table);
    }

    public function updateEstimate($p_id,$c_id,$data,$table){
        $sql = "SELECT * FROM ".$table." where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            //update
            DB::update($table)
                    ->set(array(
                    'estimate_i1'=>isset($data['estimate_i1'])?$data['estimate_i1']:"",
                    'estimate_i2'=>isset($data['estimate_i2'])?$data['estimate_i2']:"",
                    'estimate_i3'=>isset($data['estimate_i3'])?$data['estimate_i3']:"",
                    'estimate_d1'=>isset($data['estimate_d1'])?$data['estimate_d1']:"",
                    'estimate_d2'=>isset($data['estimate_d2'])?$data['estimate_d2']:"",
                    'estimate_d3'=>isset($data['estimate_d3'])?$data['estimate_d3']:"",
                    'estimate_d4'=>isset($data['estimate_d4'])?$data['estimate_d4']:"",
                    'estimate_d5'=>isset($data['estimate_d5'])?$data['estimate_d5']:"",
                    'estimate_d6'=>isset($data['estimate_d6'])?$data['estimate_d6']:"",
                    'estimate_d7'=>isset($data['estimate_d7'])?$data['estimate_d7']:"",
                    'estimate_d8'=>isset($data['estimate_d8'])?$data['estimate_d8']:"",
                    'estimate_d9'=>isset($data['estimate_d9'])?$data['estimate_d9']:"",
                    'estimate_d10'=>isset($data['estimate_d10'])?$data['estimate_d10']:"",
                    'estimate_d11'=>isset($data['estimate_d11'])?$data['estimate_d11']:"",
                    'estimate_d12'=>isset($data['estimate_d12'])?$data['estimate_d12']:"",
                    'estimate_reason'=>isset($data['estimate_reason'])?$data['estimate_reason']:""
                    ))
                    ->where('p_id','=', $p_id)
                    ->and_where('c_id', '=',$c_id)
                    ->execute();
        }else {
            print_r($data);
            DB::insert($table,
                    array(
                    'p_id' ,'c_id' ,
                    'estimate_i1' ,'estimate_i2' ,'estimate_i3' ,'estimate_d1' ,'estimate_d2' ,'estimate_d3' ,
                    'estimate_d4' ,'estimate_d5' ,'estimate_d6' ,'estimate_d7' ,'estimate_d8' ,'estimate_d9' ,'estimate_d10' ,
                    'estimate_d11' ,'estimate_d12','estimate_reason'
                    ))
                    ->values(array(
                    $p_id,$c_id,
                    isset($data['estimate_i1'])?$data['estimate_i1']:"",
                    isset($data['estimate_i2'])?$data['estimate_i2']:"",
                    isset($data['estimate_i3'])?$data['estimate_i3']:"",
                    isset($data['estimate_d1'])?$data['estimate_d1']:"",
                    isset($data['estimate_d2'])?$data['estimate_d2']:"",
                    isset($data['estimate_d3'])?$data['estimate_d3']:"",
                    isset($data['estimate_d4'])?$data['estimate_d4']:"",
                    isset($data['estimate_d5'])?$data['estimate_d5']:"",
                    isset($data['estimate_d6'])?$data['estimate_d6']:"",
                    isset($data['estimate_d7'])?$data['estimate_d7']:"",
                    isset($data['estimate_d8'])?$data['estimate_d8']:"",
                    isset($data['estimate_d9'])?$data['estimate_d9']:"",
                    isset($data['estimate_d10'])?$data['estimate_d10']:"",
                    isset($data['estimate_d11'])?$data['estimate_d11']:"",
                    isset($data['estimate_d12'])?$data['estimate_d12']:"",
                    isset($data['estimate_reason'])?$data['estimate_reason']:""
                    ))
                    ->execute();
        }
    }

    public function updateStrokePhyData($p_id,$c_id,$data) {
        $table = "stroke_phy_data";
        $sql = "SELECT * FROM ".$table." where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            //update
            DB::update($table)
                    ->set(array(
                    'BPmin'=>isset($data['BPmin'])?$data['BPmin']:"",
                    'BPmax'=>isset($data['BPmax'])?$data['BPmax']:"",
                    'PR'=>isset($data['PR'])?$data['PR']:"",
                    'PRminYN'=>isset($data['PRminYN'])?$data['PRminYN']:"",
                    'CarotidLt'=>isset($data['CarotidLt'])?$data['CarotidLt']:"",
                    'CarotidRt'=>isset($data['CarotidRt'])?$data['CarotidRt']:"",
                    'Heart'=>isset($data['Heart'])?$data['Heart']:"",
                    'HeartNA'=>isset($data['HeartNA'])?$data['HeartNA']:"",
                    'ConsciousnessE'=>isset($data['ConsciousnessE'])?$data['ConsciousnessE']:"",
                    'ConsciousnessM'=>isset($data['ConsciousnessM'])?$data['ConsciousnessM']:"",
                    'ConsciousnessV'=>isset($data['ConsciousnessV'])?$data['ConsciousnessV']:"",
                    'OrientationTime'=>isset($data['OrientationTime'])?$data['OrientationTime']:"",
                    'OrientationPlace'=>isset($data['OrientationPlace'])?$data['OrientationPlace']:"",
                    'OrientationPerson'=>isset($data['OrientationPerson'])?$data['OrientationPerson']:"",
                    'Speech'=>isset($data['Speech'])?$data['Speech']:"",
                    'Aphasia'=>isset($data['Aphasia'])?$data['Aphasia']:"",
                    'Fluency'=>isset($data['Fluency'])?$data['Fluency']:"",
                    'Comprehension'=>isset($data['Comprehension'])?$data['Comprehension']:"",
                    'Paraphasia'=>isset($data['Paraphasia'])?$data['Paraphasia']:"",
                    'Repetition'=>isset($data['Repetition'])?$data['Repetition']:"",
                    'Naming'=>isset($data['Naming'])?$data['Naming']:"",
                    'CranialL'=>isset($data['CranialL'])?$data['CranialL']:"",
                    'CranialR'=>isset($data['CranialR'])?$data['CranialR']:"",
                    'Horner'=>isset($data['Horner'])?$data['Horner']:"",
                    'HornerLR'=>isset($data['HornerLR'])?$data['HornerLR']:"",                    
                    'PupilsLt'=>isset($data['PupilsLt'])?$data['PupilsLt']:"",
                    'PupilsLtc'=>isset($data['PupilsLtc'])?$data['PupilsLtc']:"",
                    'PupilsRt'=>isset($data['PupilsRt'])?$data['PupilsRt']:"",
                    'PupilsRtc'=>isset($data['PupilsRtc'])?$data['PupilsRtc']:"",
                    'VALt'=>isset($data['VALt'])?$data['VALt']:"",
                    'VARt'=>isset($data['VARt'])?$data['VARt']:"",
                    'VFLt'=>isset($data['VFLt'])?$data['VFLt']:"",
                    'VFRt'=>isset($data['VFRt'])?$data['VFRt']:"",
                    'FacialWeaknessLR'=>isset($data['FacialWeaknessLR'])?$data['FacialWeaknessLR']:"",
                    'FacialWeaknessUL'=>isset($data['FacialWeaknessUL'])?$data['FacialWeaknessUL']:"",
                    'TongueDeviationYN'=>isset($data['TongueDeviationYN'])?$data['TongueDeviationYN']:"",
                    'TongueDeviationLR'=>isset($data['TongueDeviationLR'])?$data['TongueDeviationLR']:"",
                    'GagReflexL'=>isset($data['GagReflexL'])?$data['GagReflexL']:"",
                    'GagReflexR'=>isset($data['GagReflexR'])?$data['GagReflexR']:"",
                    'UvalaMD'=>isset($data['UvalaMD'])?$data['UvalaMD']:"",
                    'UvalaLR'=>isset($data['UvalaLR'])?$data['UvalaLR']:"",
                    'MotorLTp'=>isset($data['MotorLTp'])?$data['MotorLTp']:"",
                    'MotorLTd'=>isset($data['MotorLTd'])?$data['MotorLTd']:"",
                    'MotorLBp'=>isset($data['MotorLBp'])?$data['MotorLBp']:"",
                    'MotorLBd'=>isset($data['MotorLBd'])?$data['MotorLBd']:"",
                    'MotorRTp'=>isset($data['MotorRTp'])?$data['MotorRTp']:"",
                    'MotorRTd'=>isset($data['MotorRTd'])?$data['MotorRTd']:"",
                    'MotorRBp'=>isset($data['MotorRBp'])?$data['MotorRBp']:"",
                    'MotorRBd'=>isset($data['MotorRBd'])?$data['MotorRBd']:"",
                    'PronatorDriftingLR'=>isset($data['PronatorDriftingLR'])?$data['PronatorDriftingLR']:"",
                    'CerebellarYN'=>isset($data['CerebellarYN'])?$data['CerebellarYN']:"",
                    'CerebellarGLR'=>isset($data['CerebellarGLR'])?$data['CerebellarGLR']:"",
                    'CerebellarTYN'=>isset($data['CerebellarTYN'])?$data['CerebellarTYN']:"",
                    'CerebellarFYN'=>isset($data['CerebellarFYN'])?$data['CerebellarFYN']:"",
                    'SensoryTMD'=>isset($data['SensoryTMD'])?$data['SensoryTMD']:"",
                    'SensoryHlr'=>isset($data['SensoryHlr'])?$data['SensoryHlr']:"",
                    'SensoryPlr'=>isset($data['SensoryPlr'])?$data['SensoryPlr']:"",
                    'SensoryP'=>isset($data['SensoryP'])?$data['SensoryP']:"",
                    'NeglectLt'=>isset($data['NeglectLt'])?$data['NeglectLt']:"",
                    'NeglectRt'=>isset($data['NeglectRt'])?$data['NeglectRt']:"",
                    'ProprioceptionLt'=>isset($data['ProprioceptionLt'])?$data['ProprioceptionLt']:"",
                    'ProprioceptionRt'=>isset($data['ProprioceptionRt'])?$data['ProprioceptionRt']:"",                    
                    'FinalDXHI'=>isset($data['FinalDXHI'])?$data['FinalDXHI']:"",
                    'FinalDXHType'=>isset($data['FinalDXHType'])?$data['FinalDXHType']:"",
                    'VesselAP'=>isset($data['VesselAP'])?$data['VesselAP']:"",
                    'VesselAc'=>isset($data['VesselAc'])?$data['VesselAc']:"",
                    'VesselPc'=>isset($data['VesselPc'])?$data['VesselPc']:"",
                    'Cause'=>isset($data['Cause'])?$data['Cause']:"",
                    'CauseE'=>isset($data['CauseE'])?$data['CauseE']:"",
                    'CauseLType'=>isset($data['CauseLType'])?$data['CauseLType']:"",
                    'CauseOther'=>isset($data['CauseOther'])?$data['CauseOther']:"",
                    'Stage'=>isset($data['Stage'])?$data['Stage']:"",
                    'stp_others'=>isset($data['stp_others'])?$data['stp_others']:"",
                    'ProvisionalDiagnosis'=>isset($data['ProvisionalDiagnosis'])?$data['ProvisionalDiagnosis']:""                    
                    ))
                    ->where('p_id','=', $p_id)
                    ->and_where('c_id', '=',$c_id)
                    ->execute();
        }else {
            DB::insert($table,
                    array('p_id','c_id',
                    'BPmin','BPmax','PR','PRminYN','CarotidLt','CarotidRt','Heart','HeartNA','ConsciousnessE','ConsciousnessM','ConsciousnessV',
                    'OrientationTime','OrientationPlace','OrientationPerson','Speech','Aphasia','Fluency','Comprehension','Paraphasia','Repetition','Naming',
                    'CranialL','CranialR','Horner','HornerLR','PupilsLt','PupilsLtc','PupilsRt','PupilsRtc','VALt',
                    'VARt','VFLt','VFRt','FacialWeaknessLR','FacialWeaknessUL','TongueDeviationYN','TongueDeviationLR','GagReflexL',
                    'GagReflexR','UvalaMD','UvalaLR','MotorLTp','MotorLTd','MotorLBp','MotorLBd','MotorRTp','MotorRTd','MotorRBp','MotorRBd',
                    'PronatorDriftingLR','CerebellarYN','CerebellarGLR','CerebellarTYN','CerebellarFYN','SensoryTMD','SensoryHlr','SensoryPlr','SensoryP','NeglectLt','NeglectRt',
                    'ProprioceptionLt','ProprioceptionRt',
                    'FinalDXHI','FinalDXHType','VesselAP','VesselAc','VesselPc','Cause','CauseE','CauseLType','CauseOther','Stage',
                    'stp_others','ProvisionalDiagnosis'
                    ))
                    ->values(array(
                    $p_id,$c_id,
                    isset($data['BPmin'])?$data['BPmin']:"",
                    isset($data['BPmax'])?$data['BPmax']:"",
                    isset($data['PR'])?$data['PR']:"",
                    isset($data['PRminYN'])?$data['PRminYN']:"",
                    isset($data['CarotidLt'])?$data['CarotidLt']:"",
                    isset($data['CarotidRt'])?$data['CarotidRt']:"",
                    isset($data['Heart'])?$data['Heart']:"",
                    isset($data['HeartNA'])?$data['HeartNA']:"",
                    isset($data['ConsciousnessE'])?$data['ConsciousnessE']:"",
                    isset($data['ConsciousnessM'])?$data['ConsciousnessM']:"",
                    isset($data['ConsciousnessV'])?$data['ConsciousnessV']:"",
                    isset($data['OrientationTime'])?$data['OrientationTime']:"",
                    isset($data['OrientationPlace'])?$data['OrientationPlace']:"",
                    isset($data['OrientationPerson'])?$data['OrientationPerson']:"",
                    isset($data['Speech'])?$data['Speech']:"",
                    isset($data['Aphasia'])?$data['Aphasia']:"",
                    isset($data['Fluency'])?$data['Fluency']:"",
                    isset($data['Comprehension'])?$data['Comprehension']:"",
                    isset($data['Paraphasia'])?$data['Paraphasia']:"",
                    isset($data['Repetition'])?$data['Repetition']:"",
                    isset($data['Naming'])?$data['Naming']:"",
                    isset($data['CranialL'])?$data['CranialL']:"",
                    isset($data['CranialR'])?$data['CranialR']:"",
                    isset($data['Horner'])?$data['Horner']:"",
                    isset($data['HornerLR'])?$data['HornerLR']:"",                   
                    isset($data['PupilsLt'])?$data['PupilsLt']:"",
                    isset($data['PupilsLtc'])?$data['PupilsLtc']:"",
                    isset($data['PupilsRt'])?$data['PupilsRt']:"",
                    isset($data['PupilsRtc'])?$data['PupilsRtc']:"",
                    isset($data['VALt'])?$data['VALt']:"",
                    isset($data['VARt'])?$data['VARt']:"",
                    isset($data['VFLt'])?$data['VFLt']:"",
                    isset($data['VFRt'])?$data['VFRt']:"",
                    isset($data['FacialWeaknessLR'])?$data['FacialWeaknessLR']:"",
                    isset($data['FacialWeaknessUL'])?$data['FacialWeaknessUL']:"",
                    isset($data['TongueDeviationYN'])?$data['TongueDeviationYN']:"",
                    isset($data['TongueDeviationLR'])?$data['TongueDeviationLR']:"",
                    isset($data['GagReflexL'])?$data['GagReflexL']:"",
                    isset($data['GagReflexR'])?$data['GagReflexR']:"",
                    isset($data['UvalaMD'])?$data['UvalaMD']:"",
                    isset($data['UvalaLR'])?$data['UvalaLR']:"",
                    isset($data['MotorLTp'])?$data['MotorLTp']:"",
                    isset($data['MotorLTd'])?$data['MotorLTd']:"",
                    isset($data['MotorLBp'])?$data['MotorLBp']:"",
                    isset($data['MotorLBd'])?$data['MotorLBd']:"",
                    isset($data['MotorRTp'])?$data['MotorRTp']:"",
                    isset($data['MotorRTd'])?$data['MotorRTd']:"",
                    isset($data['MotorRBp'])?$data['MotorRBp']:"",
                    isset($data['MotorRBd'])?$data['MotorRBd']:"",
                    isset($data['PronatorDriftingLR'])?$data['PronatorDriftingLR']:"",
                    isset($data['CerebellarYN'])?$data['CerebellarYN']:"",
                    isset($data['CerebellarGLR'])?$data['CerebellarGLR']:"",
                    isset($data['CerebellarTYN'])?$data['CerebellarTYN']:"",
                    isset($data['CerebellarFYN'])?$data['CerebellarFYN']:"",
                    isset($data['SensoryTMD'])?$data['SensoryTMD']:"",
                    isset($data['SensoryHlr'])?$data['SensoryHlr']:"",
                    isset($data['SensoryPlr'])?$data['SensoryPlr']:"",
                    isset($data['SensoryP'])?$data['SensoryP']:"",
                    isset($data['NeglectLt'])?$data['NeglectLt']:"",
                    isset($data['NeglectRt'])?$data['NeglectRt']:"",
                    isset($data['ProprioceptionLt'])?$data['ProprioceptionLt']:"",
                    isset($data['ProprioceptionRt'])?$data['ProprioceptionRt']:"",                    
                    isset($data['FinalDXHI'])?$data['FinalDXHI']:"",
                    isset($data['FinalDXHType'])?$data['FinalDXHType']:"",
                    isset($data['VesselAP'])?$data['VesselAP']:"",
                    isset($data['VesselAc'])?$data['VesselAc']:"",
                    isset($data['VesselPc'])?$data['VesselPc']:"",
                    isset($data['Cause'])?$data['Cause']:"",
                    isset($data['CauseE'])?$data['CauseE']:"",
                    isset($data['CauseLType'])?$data['CauseLType']:"",
                    isset($data['CauseOther'])?$data['CauseOther']:"",
                    isset($data['Stage'])?$data['Stage']:"",
                    isset($data['stp_others'])?$data['stp_others']:"",
                    isset($data['ProvisionalDiagnosis'])?$data['ProvisionalDiagnosis']:""                    
                    ))
                    ->execute();
        }
    }

    public function updateNIHSSData($p_id,$c_id,$data) {

        $sql = "SELECT * FROM nihss where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            DB::update('nihss')
                    ->set(array(
                    'no1a' =>isset($data['no1a'])?$data['no1a'] :0,
                    'no1b' =>isset($data['no1b'])?$data['no1b'] :0,
                    'no1c' =>isset($data['no1c'])?$data['no1c'] :0,
                    'no2' =>isset($data['no2'])?$data['no2'] :0,
                    'no3' =>isset($data['no3'])?$data['no3'] :0,
                    'no4' =>isset($data['no4'])?$data['no4'] :0,
                    'no5a' =>isset($data['no5a'])?$data['no5a'] :0,
                    'no5b' =>isset($data['no5b'])?$data['no5b'] :0,
                    'no6a' =>isset($data['no6a'])?$data['no6a'] :0,
                    'no6b' =>isset($data['no6b'])?$data['no6b'] :0,
                    'no7' =>isset($data['no7'])?$data['no7'] :0,
                    'no8' =>isset($data['no8'])?$data['no8'] :0,
                    'no9' =>isset($data['no9'])?$data['no9'] :0,
                    'no10' =>isset($data['no10'])?$data['no10'] :0,
                    'no11' =>isset($data['no11'])?$data['no11']:0
                    ))
                    ->where('p_id','=', $p_id)
                    ->and_where('c_id', '=',$c_id)
                    ->execute();
        }else {
            //insert
            DB::insert('nihss',
                    array('c_id','p_id', 'no1a','no1b','no1c','no2','no3','no4','no5a',
                    'no5b','no6a','no6b','no7','no8','no9','no10','no11'
                    ))
                    ->values(array(
                    $c_id,$p_id,
                    isset($data['no1a'])?$data['no1a']:0,
                    isset($data['no1b'])?$data['no1b']:0,
                    isset($data['no1c'])?$data['no1c']:0,
                    isset($data['no2'])?$data['no2']:0,
                    isset($data['no3'])?$data['no3']:0,
                    isset($data['no4'])?$data['no4']:0,
                    isset($data['no5a'])?$data['no5a']:0,
                    isset($data['no5b'])?$data['no5b']:0,
                    isset($data['no6a'])?$data['no6a']:0,
                    isset($data['no6b'])?$data['no6b']:0,
                    isset($data['no7'])?$data['no7']:0,
                    isset($data['no8'])?$data['no8']:0,
                    isset($data['no9'])?$data['no9']:0,
                    isset($data['no10'])?$data['no10']:0,
                    isset($data['no11'])?$data['no11']:0))
                    ->execute();
        }
    }

    public function updateNIHSSCData($p_id,$c_id,$data) {
        $table = "nihss_c";
        $sql = "SELECT * FROM ".$table."  where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            DB::update($table)
                    ->set(array(
                    'no1a' =>isset($data['no1a'])?$data['no1a'] :0,
                    'no1b' =>isset($data['no1b'])?$data['no1b'] :0,
                    'no1c' =>isset($data['no1c'])?$data['no1c'] :0,
                    'no2' =>isset($data['no2'])?$data['no2'] :0,
                    'no3' =>isset($data['no3'])?$data['no3'] :0,
                    'no4' =>isset($data['no4'])?$data['no4'] :0,
                    'no5a' =>isset($data['no5a'])?$data['no5a'] :0,
                    'no5b' =>isset($data['no5b'])?$data['no5b'] :0,
                    'no6a' =>isset($data['no6a'])?$data['no6a'] :0,
                    'no6b' =>isset($data['no6b'])?$data['no6b'] :0,
                    'no7' =>isset($data['no7'])?$data['no7'] :0,
                    'no8' =>isset($data['no8'])?$data['no8'] :0,
                    'no9' =>isset($data['no9'])?$data['no9'] :0,
                    'no10' =>isset($data['no10'])?$data['no10'] :0,
                    'no11' =>isset($data['no11'])?$data['no11']:0
                    ))
                    ->where('p_id','=', $p_id)
                    ->and_where('c_id', '=',$c_id)
                    ->execute();
        }else {
            //insert
            DB::insert($table,
                    array('c_id','p_id', 'no1a','no1b','no1c','no2','no3','no4','no5a',
                    'no5b','no6a','no6b','no7','no8','no9','no10','no11'
                    ))
                    ->values(array(
                    $c_id,$p_id,
                    isset($data['no1a'])?$data['no1a']:0,
                    isset($data['no1b'])?$data['no1b']:0,
                    isset($data['no1c'])?$data['no1c']:0,
                    isset($data['no2'])?$data['no2']:0,
                    isset($data['no3'])?$data['no3']:0,
                    isset($data['no4'])?$data['no4']:0,
                    isset($data['no5a'])?$data['no5a']:0,
                    isset($data['no5b'])?$data['no5b']:0,
                    isset($data['no6a'])?$data['no6a']:0,
                    isset($data['no6b'])?$data['no6b']:0,
                    isset($data['no7'])?$data['no7']:0,
                    isset($data['no8'])?$data['no8']:0,
                    isset($data['no9'])?$data['no9']:0,
                    isset($data['no10'])?$data['no10']:0,
                    isset($data['no11'])?$data['no11']:0))
                    ->execute();
        }
    }

    public function updateLaboratoryData($p_id,$c_id,$data) {
        $table = "laboratory_data";
        $sql = "SELECT * FROM ".$table." where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            //update
            DB::update($table)
                    ->set(array(
                    'CTscanYN'=>isset($data['CTscanYN'])?$data['CTscanYN']:"",
                    'CTNoICHYN'=>isset($data['CTNoICHYN'])?$data['CTNoICHYN']:"",
                    'CTnobrainYN'=>isset($data['CTnobrainYN'])?$data['CTnobrainYN']:"",
                    'CTPerfusionYN'=>isset($data['CTPerfusionYN'])?$data['CTPerfusionYN']:"",
                    'MRIYN'=>isset($data['MRIYN'])?$data['MRIYN']:"",
                    'MRIResult'=>isset($data['MRIResult'])?$data['MRIResult']:"",
                    'MRINoICHYN'=>isset($data['MRINoICHYN'])?$data['MRINoICHYN']:"",
                    'MRInobrainYN'=>isset($data['MRInobrainYN'])?$data['MRInobrainYN']:"",
                    'MRIDWI30YN'=>isset($data['MRIDWI30YN'])?$data['MRIDWI30YN']:"",
                    'MRIDWIPWIYN'=>isset($data['MRIDWIPWIYN'])?$data['MRIDWIPWIYN']:"",
                    'MRAYN'=>isset($data['MRAYN'])?$data['MRAYN']:"",
                    'MRAMCAYN'=>isset($data['MRAMCAYN'])?$data['MRAMCAYN']:"",
                    'MRAMCAso'=>isset($data['MRAMCAso'])?$data['MRAMCAso']:"",
                    'MRAACAYN'=>isset($data['MRAACAYN'])?$data['MRAACAYN']:"",
                    'MRAACAso'=>isset($data['MRAACAso'])?$data['MRAACAso']:"",
                    'MRABAYN'=>isset($data['MRABAYN'])?$data['MRABAYN']:"",
                    'MRABAso'=>isset($data['MRABAso'])?$data['MRABAso']:"",
                    'MRAICAYN'=>isset($data['MRAICAYN'])?$data['MRAICAYN']:"",
                    'MRAICAso'=>isset($data['MRAICAso'])?$data['MRAICAso']:"",
                    'MRAotherYN'=>isset($data['MRAotherYN'])?$data['MRAotherYN']:"",
                    'MRAother'=>isset($data['MRAother'])?$data['MRAother']:"",
                    'BloodPG'=>isset($data['BloodPG'])?$data['BloodPG']:"",
                    'BloodPT'=>isset($data['BloodPT'])?$data['BloodPT']:"",
                    'BloodPTT'=>isset($data['BloodPTT'])?$data['BloodPTT']:"",
                    'BloodPLT'=>isset($data['BloodPLT'])?$data['BloodPLT']:"",
                    'BloodPGYN'=>isset($data['BloodPGYN'])?$data['BloodPGYN']:"",
                    'BloodINRYN'=>isset($data['BloodINRYN'])?$data['BloodINRYN']:"",                    
                    'BloodPLTYN'=>isset($data['BloodPLTYN'])?$data['BloodPLTYN']:""
                    ))
                    ->where('p_id','=', $p_id)
                    ->and_where('c_id', '=',$c_id)
                    ->execute();
        }else {
            DB::insert($table,
                    array('p_id','c_id',
                    'CTscanYN' , 'CTNoICHYN' , 'CTnobrainYN' , 'CTPerfusionYN' ,
                    'MRIYN' , 'MRIResult' ,'MRINoICHYN' ,'MRInobrainYN' ,'MRIDWI30YN' ,'MRIDWIPWIYN' ,
                    'MRAYN' ,'MRAMCAYN' , 'MRAMCAso' ,'MRAACAYN' ,'MRAACAso' , 'MRABAYN' ,'MRABAso' ,
                    'MRAICAYN' ,'MRAICAso' , 'MRAotherYN' ,'MRAother' ,'BloodPG' ,'BloodPT' ,'BloodPTT' ,
                    'BloodPLT' , 'BloodPGYN' , 'BloodINRYN' ,'BloodPLTYN'))
                    ->values(array(
                    $p_id,$c_id,
                    isset($data['CTscanYN'])?$data['CTscanYN']:"",
                    isset($data['CTNoICHYN'])?$data['CTNoICHYN']:"",
                    isset($data['CTnobrainYN'])?$data['CTnobrainYN']:"",
                    isset($data['CTPerfusionYN'])?$data['CTPerfusionYN']:"",
                    isset($data['MRIYN'])?$data['MRIYN']:"",
                    isset($data['MRIResult'])?$data['MRIResult']:"",
                    isset($data['MRINoICHYN'])?$data['MRINoICHYN']:"",
                    isset($data['MRInobrainYN'])?$data['MRInobrainYN']:"",
                    isset($data['MRIDWI30YN'])?$data['MRIDWI30YN']:"",
                    isset($data['MRIDWIPWIYN'])?$data['MRIDWIPWIYN']:"",
                    isset($data['MRAYN'])?$data['MRAYN']:"",
                    isset($data['MRAMCAYN'])?$data['MRAMCAYN']:"",
                    isset($data['MRAMCAso'])?$data['MRAMCAso']:"",
                    isset($data['MRAACAYN'])?$data['MRAACAYN']:"",
                    isset($data['MRAACAso'])?$data['MRAACAso']:"",
                    isset($data['MRABAYN'])?$data['MRABAYN']:"",
                    isset($data['MRABAso'])?$data['MRABAso']:"",
                    isset($data['MRAICAYN'])?$data['MRAICAYN']:"",
                    isset($data['MRAICAso'])?$data['MRAICAso']:"",
                    isset($data['MRAotherYN'])?$data['MRAotherYN']:"",
                    isset($data['MRAother'])?$data['MRAother']:"",
                    isset($data['BloodPG'])?$data['BloodPG']:"",
                    isset($data['BloodPT'])?$data['BloodPT']:"",
                    isset($data['BloodPTT'])?$data['BloodPTT']:"",
                    isset($data['BloodPLT'])?$data['BloodPLT']:"",
                    isset($data['BloodPGYN'])?$data['BloodPGYN']:"",
                    isset($data['BloodINRYN'])?$data['BloodINRYN']:"",                    
                    isset($data['BloodPLTYN'])?$data['BloodPLTYN']:""
                    ))
                    ->execute();
        }
    }

    public function updateStokeData($p_id,$c_id,$data) {
        $sql = "SELECT * FROM stroke_data where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            //update
            DB::update('stroke_data')
                    ->set(array(
                    'strokeTimeYN'=>isset($data['strokeTimeYN'])?$data['strokeTimeYN']:"",
                    'strokeTimeHour'=>isset($data['strokeTimeHour'])?$data['strokeTimeHour']:"",
                    'strokeTimeMinute'=>isset($data['strokeTimeMinute'])?$data['strokeTimeMinute']:"",
                    'strokeTimeDMY'=>isset($data['strokeTimeDMY'])?$data['strokeTimeDMY']:"",
                    'strokeNormalTimeYN'=>isset($data['strokeNormalTimeYN'])?$data['strokeNormalTimeYN']:"",
                    'strokeNormalTimeHour'=>isset($data['strokeNormalTimeHour'])?$data['strokeNormalTimeHour']:"",
                    'strokeNormalTimeMinute'=>isset($data['strokeNormalTimeMinute'])?$data['strokeNormalTimeMinute']:"",
                    'strokeNormalTimeDMY'=>isset($data['strokeNormalTimeDMY'])?$data['strokeNormalTimeDMY']:"",
                        
                    'cc'=>$data['cc'],
                    'PI'=>$data['PI'],
                    //Remove
                    //'headache'=>isset($data['headache'])?$data['headache']:"",
                    //'sideheadache'=>isset($data['sideheadache'])?$data['sideheadache']:"",
                    //'muchheadache'=>isset($data['muchheadache'])?$data['muchheadache']:"",
                    //'areaheadache'=>isset($data['areaheadache'])?$data['areaheadache']:"",
                    //'areaheadache_other'=>isset($data['areaheadache_other'])?$data['areaheadache_other']:"",
                    'ph_ht_yn'=>isset($data['ph_ht_yn'])?$data['ph_ht_yn']:"",
                    'ph_ht_yrs'=>isset($data['ph_ht_yrs'])?$data['ph_ht_yrs']:"",
                    'ph_ht_med'=>isset($data['ph_ht_med'])?$data['ph_ht_med']:"",
                    'ph_dm_yn'=>isset($data['ph_dm_yn'])?$data['ph_dm_yn']:"",
                    'ph_dm_yrs'=>isset($data['ph_dm_yrs'])?$data['ph_dm_yrs']:"",
                    'ph_dm_med'=>isset($data['ph_dm_med'])?$data['ph_dm_med']:"",
                    'ph_dyslipidemia_yn'=>isset($data['ph_dyslipidemia_yn'])?$data['ph_dyslipidemia_yn']:"",
                    'ph_dyslipidemia_yrs'=>isset($data['ph_dyslipidemia_yrs'])?$data['ph_dyslipidemia_yrs']:"",
                    'ph_dyslipidemia_med'=>isset($data['ph_dyslipidemia_med'])?$data['ph_dyslipidemia_med']:"",
                    'ph_smoking_yn'=>isset($data['ph_smoking_yn'])?$data['ph_smoking_yn']:"",
                    'ph_smoking_pack'=>isset($data['ph_smoking_pack'])?$data['ph_smoking_pack']:"",
                    'ph_smoking_yrs'=>isset($data['ph_smoking_yrs'])?$data['ph_smoking_yrs']:"",
                    'ph_smoking_qyrs'=>isset($data['ph_smoking_qyrs'])?$data['ph_smoking_qyrs']:"",
                    'ph_heart_disease_yn'=>isset($data['ph_heart_disease_yn'])?$data['ph_heart_disease_yn']:"",
                    'ph_heart_disease_type'=>isset($data['ph_heart_disease_type'])?$data['ph_heart_disease_type']:"",
                    'ph_heart_disease_med'=>isset($data['ph_heart_disease_med'])?$data['ph_heart_disease_med']:"",
                    'ph_others_yn'=>isset($data['ph_others_yn'])?$data['ph_others_yn']:"",
                    'ph_others'=>isset($data['ph_others'])?$data['ph_others']:"",
                    //New
                    'ph_stroke_yn'=>isset($data['ph_stroke_yn'])?$data['ph_stroke_yn']:"",
                    'ph_stroke_year'=>isset($data['ph_stroke_year'])?$data['ph_stroke_year']:"",
                    'ph_stroke_type'=>isset($data['ph_stroke_type'])?$data['ph_stroke_type']:"",
                    ))
                    ->where('p_id','=', $p_id)
                    ->and_where('c_id', '=',$c_id)
                    ->execute();
        }else {
            //insert
            DB::insert('stroke_data',
                    array('p_id','c_id','cc','PI',
                    'ph_ht_yn','ph_ht_yrs','ph_ht_med','ph_dm_yn',
                    'ph_dm_yrs','ph_dm_med','ph_dyslipidemia_yn','ph_dyslipidemia_yrs',
                    'ph_dyslipidemia_med','ph_smoking_yn','ph_smoking_pack','ph_smoking_yrs',
                    'ph_smoking_qyrs','ph_heart_disease_yn','ph_heart_disease_type','ph_heart_disease_med',
                    'ph_others_yn','ph_others'))
                    ->values(array(
                    $p_id,$c_id,$data['cc'],$data['PI'],
                    $data['ph_ht_yn'],$data['ph_ht_yrs'],
                    $data['ph_ht_med'],$data['ph_dm_yn'],$data['ph_dm_yrs'],$data['ph_dm_med'],$data['ph_dyslipidemia_yn'],
                    $data['ph_dyslipidemia_yrs'],$data['ph_dyslipidemia_med'],$data['ph_smoking_yn'],$data['ph_smoking_pack'],
                    $data['ph_smoking_yrs'],$data['ph_smoking_qyrs'],$data['ph_heart_disease_yn'],$data['ph_heart_disease_type'],
                    $data['ph_heart_disease_med'],$data['ph_others_yn'],$data['ph_others']))
                    ->execute();
        }
    }

    public function updateToStartStatus($p_id,$c_id) {
        $this->updateStatus($p_id, $c_id,"starting");
    }

    public function updateToWaitingToConsultStatus($p_id,$c_id,$consult_doctor_id) {
        $sql = "SELECT * FROM consult_status where p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)==0) {
            DB::insert('consult_status',array('p_id','c_id','status','consult_start'))
                    ->values(array($p_id,$c_id,"Waiting to consult",DB::expr('NOW()')))->execute();
        }else {
            DB::update('consult_status')

                    ->set(array('status'=>"Waiting to consult",'consult_doctor_id'=>$consult_doctor_id))
                    ->where('c_id','=', $c_id)
                    ->execute();
        }
    }

    public function checkStatus($p_id,$c_id) {
        $sql = "SELECT status FROM consult_status where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0]['status'];
        }else {
            return null;
        }
    }

    public function getConsultDoctorID($p_id,$c_id){
         $sql = "SELECT consult_doctor_id FROM consult_status where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
            return $query[0]['consult_doctor_id'];
        }else {
            return null;
        }
    }

    public function endConsult($p_id,$c_id) {
        $sql = "SELECT * FROM consult_status where c_id=".$c_id." and p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        $c_id = $query[0]['c_id'];
        $p_id = $query[0]['p_id'];
        if(count($query)!=0) {
            DB::insert("consult_history",array('c_id','p_id','pp_id','consult_start','consult_end',
                    'consult_doctor_id','doctor_name'
                    ))->values(array(
                    $query[0]['c_id'],$query[0]['p_id'],$query[0]['pp_id'],$query[0]['consult_start'],DB::expr('NOW()'),
                    $query[0]['consult_doctor_id'],$query[0]['doctor_name']
                    ))->execute();

            DB::delete("consult_status")
                    ->where('c_id', '=',$c_id)
                    ->and_where('p_id', '=', $p_id)
                    ->execute();
        }else {
            return null;
        }
    }

    public function updateToConsultingStatus($p_id) {
        $c_id = $this->getConsultID($p_id);
        $this->updateStatus($p_id, $c_id,"Consulting");
    }
    
    
    
    
}
?>
